package com.google.mr.okhttptest.itemfragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.google.mr.okhttptest.R;
import com.google.mr.okhttptest.StoryDetailActivity;
import com.google.mr.okhttptest.adapter.CommonRecyclerViewAdapter;
import com.google.mr.okhttptest.bean.ZhihuBean;
import com.google.mr.okhttptest.fragment.BaseFragment;
import com.google.mr.okhttptest.net.Contants;
import com.google.mr.okhttptest.net.JsonGenericsSerializator;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.GenericsCallback;

import butterknife.BindView;
import okhttp3.Call;
import okhttp3.Request;

import static com.google.mr.okhttptest.net.Contants.BEFORENEWS;
import static com.google.mr.okhttptest.net.Contants.LASTESTNEWS;

/**
 * Created by Administrator on 2017/11/27.
 */

public class recommendFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.rec_recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.rec_srl)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private CommonRecyclerViewAdapter commonRecyclerViewAdapter;

    private String mCurrentDate;

    private RecyclerView.OnScrollListener MyScrollListener=new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState==RecyclerView.SCROLL_STATE_IDLE&&(linearLayoutManager.findLastVisibleItemPosition()==commonRecyclerViewAdapter.getItemCount()-1)){
                OkHttpUtils.get()
                        .url(Contants.URLZHIHU+BEFORENEWS+mCurrentDate)
                        .id(100)
                        .build()
                        .execute(new GenericsCallback<ZhihuBean>(new JsonGenericsSerializator()) {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Toast.makeText(mContext,"服务器忙,请稍后再试!",Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onResponse(ZhihuBean response, int id) {
                                commonRecyclerViewAdapter.addStories(response);
                                mCurrentDate = response.getDate();
                            }
                        });
            }

        }
    };
    private LinearLayoutManager linearLayoutManager;

    @Override
    public int getContentViewLayoutID() {
        return R.layout.fragment_item_recommend;
    }

    @Override
    protected void initView() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerview.setLayoutManager(linearLayoutManager);
        commonRecyclerViewAdapter = new CommonRecyclerViewAdapter(getActivity());
        mRecyclerview.setAdapter(commonRecyclerViewAdapter);
        mRecyclerview.addOnScrollListener(MyScrollListener);

        commonRecyclerViewAdapter.setTopStoryItemClickListener(new CommonRecyclerViewAdapter.TopStoryItemClickListener() {
            @Override
            public void onStoryItemClick(ZhihuBean.TopStoriesBean topStoriesBean) {

                Logger.i("topStoriesBean.getId():"+topStoriesBean.getId());
                startActivity(StoryDetailActivity.getIntent(mContext,topStoriesBean.getId()));
            }
        });

        commonRecyclerViewAdapter.setStoryItemClickListener(new CommonRecyclerViewAdapter.StoryItemClickListener() {
            @Override
            public void onStoryItemClick(ZhihuBean.StoriesBean StoriesBean) {
                Logger.i("StoriesBean.getId():"+StoriesBean.getId());
                startActivity(StoryDetailActivity.getIntent(mContext,StoriesBean.getId()));
            }
        });
    }

    @Override
    public void initData() {
        OkHttpUtils.get()
                .url(Contants.URLZHIHU+LASTESTNEWS)
                .id(100)
                .build()
                .execute(new GenericsCallback<ZhihuBean>(new JsonGenericsSerializator()) {

                    @Override
                    public void onBefore(Request request, int id) {
                        super.onBefore(request, id);
                        mSwipeRefreshLayout.setRefreshing(true);
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(mContext,"服务器忙,请稍后再试!",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(ZhihuBean response, int id) {
                        Toast.makeText(mContext, "数据刷新成功", Toast.LENGTH_SHORT).show();
                        mSwipeRefreshLayout.setRefreshing(false);
                        commonRecyclerViewAdapter.setData(response);
                        mCurrentDate = response.getDate();
                        Logger.i(""+mCurrentDate);
                    }
                });
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
